/*
 * This is the main entrance of the project.
 * 
 * In this class, we create 4 normal window services, 1 express window service, 1 VIP window service.
 * 
 * Every one second, generate a normal customer.
 * Every 6 seconds, generate a VIP customer.
 * Every 3 seconds, generate a express customer. 
 * Copy right with charles. You are free to use and modify the code. Please contact author - charles@webcheng.net for commercial use.
 * */

package net.webcheng;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class BankQueue {
	public static void main(String args[]){
		//Generate 4 normal service windows.
		ServiceWindow normalSW1 = new ServiceWindow();
		normalSW1.setwType(TicketType.COMMON);
		Thread normalTd1 = new Thread(normalSW1);
		ServiceWindow normalSW2 = new ServiceWindow();
		normalSW2.setwType(TicketType.COMMON);
		Thread normalTd2 = new Thread(normalSW2);
		ServiceWindow normalSW3 = new ServiceWindow();
		normalSW3.setwType(TicketType.COMMON);
		Thread normalTd3 = new Thread(normalSW3);
		ServiceWindow normalSW4 = new ServiceWindow();
		normalSW4.setwType(TicketType.COMMON);
		Thread normalTd4 = new Thread(normalSW4);
		
		//Generate one express service window.
		ServiceWindow expressSW = new ServiceWindow();
		expressSW.setwType(TicketType.EXPRESS);
		Thread expressTd = new Thread(expressSW);
		
		//Generate one VIP service window.
		ServiceWindow vipSW = new ServiceWindow();
		vipSW.setwType(TicketType.VIP);
		Thread vipTd = new Thread(vipSW);
		
		//Set the Service window Thread names. The names will be printed out in console.
		normalTd1.setName("Normal TD1");
		normalTd1.start();
		normalTd2.setName("Normal TD2");
		normalTd2.start();
		normalTd3.setName("Normal TD3");
		normalTd3.start();
		normalTd4.setName("Normal TD4");
		normalTd4.start();
		
		expressTd.setName("express TD");
		expressTd.start();
		
		vipTd.setName("VIP TD");
		vipTd.start();
		
		//Generate a normal customer every second.
		ScheduledExecutorService commonCustomers = Executors.newScheduledThreadPool(1);
		commonCustomers.scheduleAtFixedRate(new Runnable(){
			public void run(){
				TicketMachine tm = TicketMachine.getInstance();
				Ticket t = tm.generateTicket(TicketType.COMMON);
				System.out.println("Common customer "+t.getSequenceNo()+" is waiting.");
			}
		}, 0, CONSTANTS.TIME_INTERVAL, TimeUnit.SECONDS);
		
		//Generate a express customer every 3 seconds.
		ScheduledExecutorService expressCustomers = Executors.newScheduledThreadPool(1);
		expressCustomers.scheduleAtFixedRate(new Runnable(){
			public void run(){
				TicketMachine tm = TicketMachine.getInstance();
				Ticket t = tm.generateTicket(TicketType.EXPRESS);
				System.out.println("Express customer "+t.getSequenceNo()+" is waiting.");
			}
		}, 0, CONSTANTS.TIME_INTERVAL*3, TimeUnit.SECONDS);
		
		//Generate a VIP customer every 6 seconds.
		ScheduledExecutorService VIPCustomers = Executors.newScheduledThreadPool(1);
		VIPCustomers.scheduleAtFixedRate(new Runnable(){
			public void run(){
				TicketMachine tm = TicketMachine.getInstance();
				Ticket t = tm.generateTicket(TicketType.VIP);
				System.out.println("VIP customer "+t.getSequenceNo()+" is waiting.");
			}
		}, 0, CONSTANTS.TIME_INTERVAL*6, TimeUnit.SECONDS);
	}
}
